package main

import (
	"io/ioutil"
	"log"
	"fmt"
	"encoding/csv"
	"strings"
	"io"
	"os"
	"time"
)

func main() {
	//记录开始时间
	start := time.Now()
	insertData()

	//记录结束时间
	//end := time.Now().UnixNano()
	//输出执行时间，单位为毫秒。
	fmt.Println(time.Since(start))
}


func insertData() {

	dat, err := ioutil.ReadFile("return_time.csv")
	if err != nil {
		log.Fatal(err)
	}
	var f *os.File
	var file ="./return_time.sql"
	r := csv.NewReader(strings.NewReader(string(dat[:])))

	//判断文件是否存在
	f, err = os.OpenFile(file, os.O_TRUNC|os.O_CREATE|os.O_APPEND, 0666) //打开文件
	if err != nil {
		fmt.Println("打开文件失败")
	}

	for {
		record, err := r.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}

		uid := record[0]
		var sql = fmt.Sprintf("UPDATE investigation_user u, investigation_log l SET u.plan_return_date = LEFT (substring_index(l.answer2, ':' ,- 1),10) WHERE u.uid = l.uid AND u.uid = '%s' AND LOCATE('2020', l.answer2) > 0 AND ISNULL(u.plan_return_date);\r\n", uid)

		io.WriteString(f, sql) //写入文件(字符串)

		fmt.Println(sql)
	}

	f.Close()


}
